import { options } from "less";
import Mock, { mock } from "mockjs";
// 定义请求时常
Mock.setup({
    timeout:200, //200毫秒后返回数据
})

// login登录接口
Mock.mock('/login' , 'post' , (options) =>{
    // console.log(JSON.parse(options.body));
    // return JSON.parse(options.body)
    let {name , pwd} = JSON.parse(options.body);
    let token = '';
    let role = '';
    if(name == 'admin' && pwd == '123456'){
        //说明你是管理员
        return {
            code:200,
            msg:'登录请求成功',
            data:{
                token:'admin--123456',
                role:"管理员"
            }
        }
    }else if(name == 'user' && pwd == '123456'){
        // 说明你是用户
        return {
            code:200,
            msg:'登录请求成功',
            data:{
                token:'user--123456',
                role:"普通用户"
            }
        }
    }else{
        return{
            code:500,
            msg:'帐号或密码不存在',
            data:null
        }
    }
})


// 根据不同的用户返回不同的数据
Mock.mock('/getpress' , 'post' , (options) =>{
    let {role} = JSON.parse(options.body)
    console.log(role);
    if(role == '管理员'){
        return {
            code:200,
            msg:'请求成功',
            data:[
               {path:'shenpi' , 
                 meta:{
                   title:'审批管理',
                   icon:'el-icon-view'
                },
                name:'Shenpi'  
               },
               {path:'woqingjia' ,
               meta:{
                  title:'我要请假',
                  icon:'el-icon-medal-1'
               },
               name:'Woqingjia'      
               },
               {path:'qingjia' , 
                meta:{
                   title:'请假审批',
                   icon:'el-icon-notebook-1'
                },
                name:'Qingjia'       
               },
        
            ]
        }
    }else{
        return {
            code:200,
            msg:'请求成功',
            data:[
               {path:'woqingjia' ,
                meta:{
                   title:'我要请假',
                   icon:'el-icon-medal-1'
                },
                name:'Woqingjia'       
               }
            ]
        }
    }
     
})

















// 定义用户列表数据
let List =[]; //返回的数据

for (let i =0; i < 1000 ; i++){
    List.push({
        id:i,
        name:Mock.Random.cname(),
        age:Mock.Random.integer(20 ,30),
        major:'html5',
        address:Mock.Random.county(true),
        pay:Mock.Random.integer(0 , 1),
        graduateTime:Mock.Random.date('yyyy-MM-dd')
    
    })
}
// 请求用户列表数据
Mock.mock('/getUser' , 'get' , (options) =>{
   
 let {inputVal, page , size} = JSON.parse(options.body)
//  console.log(inputVal, page , size);

    let newlist = List.filter((item) =>{
        return item.name.indexOf(inputVal) != -1
    })
    // console.log(newlist);
    let newlist1 = newlist.slice((page-1) * size , page*size)
    return {
        code:200,
        msg:'请求成功',
        data:newlist1,
        totalpage:List.length,
        size:size,
        currentPage:page
        
     }
})


// 删除数据请求
Mock.mock(new RegExp('/deleteUser.*'), 'get', (options)=>{
    console.log(options);
    let id = JSON.parse(options.body).id;
    console.log(JSON.parse(options.body));

    let list1 = List.filter((item)=>{
       return item.id  != id
    })

    List = list1

    return {
        code:200,
        msg:'删除成功',
        data:null
    }
})






// 修改数据请求
Mock.mock('/editUser', 'post', (options)=>{
  
    // console.log(JSON.parse(options.body));

    let {
    address,
    age,
    graduateTime,
    id,
    major,
    name,
    pay} = JSON.parse(options.body)
  
    List.forEach(item =>{
        if(item.id === id){

            item.address = address;
            item.age = age;
            item.graduateTime = graduateTime;
            item.id = id;
            item.major = major;
            item. name =  name;
            item.pay = pay;
            
            console.log(item);
               
        }

    })
    return{
        code:200,
        msg:'修改成功',
        data:null
    }
    // console.log(List);
})


//6.柱形图接口
Mock.mock('/getBarData', 'get', (options)=>{
    return {
        code: 200,
        msg: "柱形图数据",
        data: {
            name: "出勤率",
            name2:'分数',
            xAxisData: ["周一", "周二", "周三", "周四", "周五", "周六","周日"],
            seriesData: [90, 85, 50, 70, 100 , 92,20],
            scoreData:[76, 64, 50, 70, 1 , 62,20],
        }
    }
})
//7.饼形图接口
Mock.mock('/getPieData', 'get',  (options)=>{
    return {
        code: 200,
        msg: "饼形图数据",
        data: {
            name: "",
            seriesData: [{
                // 数据项的名称
                name: "HTML5",
                // 数据项值8
                value: 500,
            },
            {
                // 数据项的名称
                name: "JAVA",
                // 数据项值8
                value: 200,
            },
            {
                // 数据项的名称
                name: "大数据",
                // 数据项值8
                value: 100,
            },
            {
                // 数据项的名称
                name: "unity",
                // 数据项值8
                value: 50,
            },
            {
                // 数据项的名称
                name: "UI",
                // 数据项值8
                value: 50,
            }]
        }
    }
})
